<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

if (empty($action)) {
	$action = "";
} else {
	$action = strtolower($action);
}

// Init
if (empty($id)) {
	$id = null;
}
if (empty($published)) {
	$published=0;
}

switch($action) {
	case "new":
		display_edit_form($id);
		break;
	case "edit":
		display_edit_form($id);
		break;
	case "save":
		save($id, $title, $intro, $published);
		break;
	case "delete":
		delete($id);
		break;
	case "publish":
		publish($id);
		break;
	default:
		display_sections();
		break;
}

function publish ($id) {
	$query = "SELECT published FROM wps_sections WHERE id='$id'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);

	if ($row->published) {
		$query = "UPDATE wps_sections SET published=0 WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	} else {
		$query = "UPDATE wps_sections SET published=1 WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_sections();
}

function delete ($id) {
	if (is_array($id)) {
		foreach($id as $value) {
			$query = "DELETE FROM wps_sections WHERE id = '$value'";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		}
	} else {
		$query = "DELETE FROM wps_sections WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_sections();
}

function save($id, $title, $intro, $published) {
	// Replace quotes
	if (!get_magic_quotes_gpc()) {
		$title = str_replace("'","\'",$title);
		$intro = str_replace("'","\'",$intro);
	}
	// Replace new lines
	//$intro = ereg_replace("(\r\n|\n|\r)", "<br />", $intro);
	$intro  = preg_replace("/(\015\012)|(\015)|(\012)/","<br />",$intro);
	if ($id) {
		$query = "UPDATE wps_sections SET title = '$title', intro = '$intro', published='$published' WHERE id = '$id'";
	} else {
		$query = "INSERT INTO wps_sections VALUES ('', '$title', '$intro', '$published')";
	}
	mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	display_sections();
}

function display_edit_form($id) {
	global $common;

	if (isset($id)) {
		if (is_array($id)) $id = $id[0];
		$query = "SELECT * FROM wps_sections WHERE id = '$id'";
		$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		$row = mysql_fetch_object($result);
	}
	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt;
    	<a href=\"index.php?option=sections\" border=0>Section Management</a> &gt;
    	Add / Edit</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Sections</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td valign=\"top\">\n";
    echo "<br>\n";
	echo "<form name=\"form\" method=\"post\" action=\"index.php?option=sections\" onsubmit=\"return validate()\">\n";
	echo "<b>Title:</b>\n";
	echo "<br>\n";
    echo "<input type=\"text\" name=\"title\" size=\"40\" value=\"";
    if (isset($id)) echo $row->title;
    echo "\">\n";
    echo "<br>\n";
	echo "<b>Description:</b>\n";
	echo "<br>\n";
    echo "<textarea name=\"intro\" rows=\"8\" cols=\"60\">";
    if (isset($id)) {
    	$row->intro = str_replace("<br />","\r\n",$row->intro);
    	echo $row->intro;
    }
    echo "</textarea>\n";
    echo "<br>\n";
    echo "Published: <input type=\"checkbox\" name=\"published\" value=\"1\"";
    if (empty($id) || $row->published==1) echo " checked";
    echo ">\n";
    echo "<br>\n";
    echo "<br>\n";
    echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	echo "<tr>\n";
	echo "<td>\n";
    if (isset($id))
    	echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
    echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
    echo "<input type=\"button\" name=\"Cancel\" value=\"Cancel\" onclick=\"document.location.href='index.php?option=sections'\">\n";
	echo "</td>\n";
	echo "<td align=\"right\">\n";
	$common->setup_default_emulator();
	$common->display_emulator();
	echo "</td>\n";
	echo "</tr>\n";
    echo "</table>";
    echo "</form>\n";
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}

function display_sections() {
	global $limit, $limitstart, $common;

	// Total
	$query = "SELECT * FROM wps_sections";
	$result = mysql_query($query) or die("Query failed");
	$total = mysql_num_rows ($result) or die ("Query failed");

	// Select query
	if (empty($limitstart)) $limitstart = 0;
	if (empty($limit)) $limit = 10;
	if ($limit > $total) {
		$limitstart = 0;
	}
	$query = "SELECT * FROM wps_sections ORDER BY title LIMIT $limitstart, $limit";
	$result = mysql_query ($query);

	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt; Section Management</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Sections</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td valign=\"top\">\n";
    echo "<br>\n";
	echo "<form name=\"form\" method=\"post\" action=\"index.php?option=sections\">\n";
	echo "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
    echo "<tr>\n";
	echo "<td><input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll();\"></td>\n";
	echo "<td width=\"80%\"><b>Title</b></td>\n";
	echo "<td width=\"20%\" align=\"center\"><b>Published</b></td>\n";
    echo "</tr>\n";
    echo "<tr><td colspan=\"3\">&nbsp;</td></tr>\n";
	for($i = 1; $row = mysql_fetch_object ($result); $i++) {
	    echo "<tr>\n";
		echo "<td><input name=\"id[]\" type=\"checkbox\" value=\"$row->id\"></td>\n";
		echo "<td><a href=\"index.php?option=sections&action=edit&id=$row->id\">$row->title</a></td>\n";
		if ($row->published) {
			echo "<td align=\"center\"><a href=\"index.php?option=sections&action=publish&id=$row->id\"><img src=\"images/publish_tick.png\" alt=\"Unpublish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		} else {
			echo "<td align=\"center\"><a href=\"index.php?option=sections&action=publish&id=$row->id\"><img src=\"images/publish_x.png\" alt=\"Publish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		}
		echo "</tr>\n";
	}
	echo "<tr><td colspan=\"3\">&nbsp;</td></tr>\n";
	echo "<tr><td colspan=\"3\" align=\"center\">";
	writePagesLinks($limitstart, $limit, $total, "sections");
	echo "</td></tr>\n";
	echo "<tr><td colspan=\"3\" align=\"center\">";
	writePagesCounter($limitstart, $limit, $total);
	echo "</td></tr>\n";
    echo "<tr>\n";
    echo "<td colspan=\"2\">
		<input type=\"submit\" name=\"action\" value=\"New\">
		<input type=\"submit\" name=\"action\" value=\"Edit\">
		<input type=\"submit\" name=\"action\" value=\"Delete\" onclick=\"return confirm_delete();\">\n";
	echo "</td>\n";
	echo "<td align=\"right\">\n";
	$common->setup_default_emulator();	
	$common->display_emulator();
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</form>\n";
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}
?>